package com.example.homework04.controller;


import com.github.pagehelper.PageInfo;
import com.example.homework04.entity.Student;
import com.example.homework04.service.StudentService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    private static final Logger logger = LogManager.getLogger(StudentController.class);


    @Autowired
    StudentService service;

    /**
     * 条件查询
     *
     * @param student
     * @param model
     * @return
     */
    @RequestMapping("/byConditions")
    public String getStudentsByConditions(Student student, Model model) {
        List<Student> students = service.queryByConditions(student);
        model.addAttribute("students", students);
        return "studentInfo";
    }


    /**
     * 返回标准响应数据对象 其中包含分页数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/byPage")
    public ResponseEntity<PageInfo<Student>> selectByPage(@RequestParam(defaultValue = "1") int pageNum,
                                                          @RequestParam(defaultValue = "10") int pageSize) {
        logger.info("selectByPage called with pageNum={}, pageSize={}", pageNum, pageSize);
        PageInfo<Student> pageInfo = service.selectByPage(pageNum, pageSize);
        return ResponseEntity.ok(pageInfo);
    }


}
